package {
	
	import flash.display.*;
	import flash.events.*;
	import flash.ui.*;
	import flash.geom.*;
	import flash.media.*;
	import flash.text.StaticText;
	import flash.utils.Timer;
	import com.combiform.core.*;
	import com.combiform.harness.Combi_Harness;
	
	public class LEDRoulette extends MovieClip {
		//globals
		const WIDTH:int=600, HEIGHT:int=300;
		
		//api
		var ioHandler:myIO_Handler;
		var cHarness:Combi_Harness;
		var co:Combine_Obj;

		var debug:Boolean = false;
		
		var STATE:int = 0;
		
		public var scoreArr:Array;
		public var selectedLED:MovieClip;
		
		public var LEDArray:Array;
		private var SpeedTimer:Timer;
		private var idx:int = Math.random() * 4;
		private var intervalTime:Number = 1;
		private var interval:Number = 1;
		
		public var roundCount:int = 1;
		
		private var maximumRound = 5;
		
		
		public function LEDRoulette(){
			this.addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		function init(e:Event){
			this.removeEventListener(Event.ADDED_TO_STAGE, init);

			//Combiform api ** note that in the released version you probably want to take out the combiharness
			ioHandler = new myIO_Handler(this.stage,true,false);
			cHarness= new Combi_Harness(this.stage);
			addChild(cHarness);
			co=new Combine_Obj();

			//listeners
			cHarness.addEventListener(IR_Event.INPUT, irHandler);
			ioHandler.addEventListener(IR_Event.INPUT, irHandler);
			addEventListener(Event.ENTER_FRAME, onFrame);
			
			LEDArray = [LED_0, LED_1, LED_2, LED_3];
			
			scoreArr = [0, 0, 0, 0];
			
			initRoulette();
		}

		
		function initRoulette() {
			idx = Math.random() * 4;
			intervalTime = 1;
			interval = 1;
			
			trace("initRoulette");
			co.clearComb_obj();
			
			SpeedTimer = new Timer(33, 0);
			SpeedTimer.addEventListener(TimerEvent.TIMER, TimerHandler);
			SpeedTimer.start();
			
			TXT_ROUND.text = "Round " + roundCount;
			
			RtoG.visible = false;
			GtoB.visible = false;
			BtoY.visible = false;
			YtoR.visible = false;
			
			RtoB.visible = false;
			GtoY.visible = false;
		}
		
		
		//main loop: ioHandler needs to call frameH so make sure this is in your main loop
		
		function TimerHandler(e:TimerEvent):void {
			
			//trace(SpeedTimer.currentCount);
			if (SpeedTimer.currentCount > 30) {
				//intervalTime = int(SpeedTimer.currentCount * SpeedTimer.currentCount / 10000 );
				intervalTime = int(SpeedTimer.currentCount * SpeedTimer.currentCount *29 / 21600 +       30 - (22500*29 / 21600));
			}
		}
		
		
		
		
		function onFrame(e:Event) : void {
			ioHandler.frameH();
			
			
			switch(STATE) {
				case 0:
				if (interval > 0) {
					interval--;
					
				}else if (interval == 0) {
					
					for (var i:int = 0; i < LEDArray.length; i++){
						LEDArray[i].gotoAndStop("off");
						//Turn of all LED
					}
					
					idx++;
					idx %= 4;
					LEDArray[idx].gotoAndPlay("on");	//turn on one LED
					
					interval = intervalTime;
					//trace(intervalTime);
					if (SpeedTimer.currentCount > 150) {	//End Condition
						
						selectedLED = LEDArray[idx];
						trace("idx", idx)
						trace("selectedLED", selectedLED.name);
						STATE = 1;
						
						trace("STATE 1 : now combine!");
						
						SpeedTimer.stop();
						SpeedTimer.removeEventListener(TimerEvent.TIMER, TimerHandler);
					}
				}
				break;
			case 1:
				
				//check if there are 2 controllers combined with selectedLED
				selectedLED.play();
				//selected LED blinks here
				
				
			break;
			case 2:
				trace("STATE 2 : combine finished");
				//players corresponding to the combined controller gets points and displays on the screen
				var match_arr:Array = [];
				for (i = 0; i < 4; i++) {
					if (i != Number(selectedLED.name.charAt(4))){
						if (co.isInAGroupWith(Number(selectedLED.name.charAt(4)), i)) {
							match_arr.push(i);
							trace("match_arr", i);
						}
					}
				}
				
				for (i = 0; i < match_arr.length ; i++) {
					
					scoreArr[match_arr[i]] = scoreArr[match_arr[i]] + 100;
				}
				
				STATE = 3;
				break;
			case 3:
				
				//Go to State 4 when every controller is decombined.
				
				
			break;
			case 4:
			if (roundCount == maximumRound) {
				trace("Game End");
				STATE = 5;
				
			}else {
				
				trace("Round ", roundCount);
				roundCount++;
				initRoulette();
				STATE = 0;
				
			}
			break;
			case 5:
			trace("Pressed mouse to restart");
			break;
			}
			
			TXT_R.text = "R: " + scoreArr[0];
			TXT_G.text = "G: " + scoreArr[1];
			TXT_B.text = "B: " + scoreArr[2];
			TXT_Y.text = "Y: " + scoreArr[3];
			
			
		}
		
      //irHandler is how your game will process IR_Events
		function irHandler(e:IR_Event):void{
			switch(e.key){
				case IR_Event.IR_ACTIONBUTTON:
					//trace ("Main:"+e.controllerNum+":KNOB e.IR_ACTIONBUTTON val="+e.val)
					break;
					
				case IR_Event.IR_ACTIONBUTTON2:
					//trace ("FXXX:" + e.controllerNum + ":KNOB e.IR_ACTIONBUTTON val=" + e.val)
					
					break;
					
				case IR_Event.IR_LEFTBUTTON:
//					//trace ("Main:e.IR_LEFTBUTTON val="+e.val)
					break;
				case IR_Event.IR_RIGHTBUTTON:
//					trace ("Main:e.IR_RIGHTBUTTON val="+e.val)
					break;
				case IR_Event.IR_DOWNBUTTON:
//					trace ("Main:e.IR_DOWNBUTTON val="+e.val)
					break;
				case IR_Event.IR_UPBUTTON:
//					trace ("Main:e.IR_UPBUTTON val="+e.val)
					break;
				case IR_Event.IR_PRECOMBINEBUTTON:
//					trace ("Main:e.IR_PRECOMBINEBUTTON val="+e.val)
					break
				case IR_Event.IR_LED:
					if(e.val == 0){
						LED_0.gotoAndStop("off");
					}else {
						LED_0.gotoAndPlay("on");
					}
					
					break;
				case IR_Event.IR_LED1:
					if(e.val == 0){
						LED_1.gotoAndStop("off");
					}else {
						LED_1.gotoAndPlay("on");
					}
					
					break;
				case IR_Event.IR_LED2:
					if(e.val == 0){
						LED_2.gotoAndStop("off");
					}else {
						LED_2.gotoAndPlay("on");
					}
					
					break;
				case IR_Event.IR_LED3:
					if(e.val == 0){
						LED_3.gotoAndStop("off");
					}else {
						LED_3.gotoAndPlay("on");
					}
					ioHandler.sendEvent(e);
					//trace("Main sent LED event");
					break;
				case IR_Event.IR_COMBINEBUTTON:
					
					co = e.combine_obj.clone();
					
					
					
					trace ("Main:e.IR_COMBINEBUTTON", co.toString());
					
					if (co.connectedto(0, 1) || co.connectedto(1, 0)) {
						RtoG.visible = true;
					}else {
						RtoG.visible = false;
					}
					if (co.connectedto(1, 2) || co.connectedto(2, 1)) {
						GtoB.visible = true;
					}else {
						GtoB.visible = false;
					}
					if (co.connectedto(2, 3) || co.connectedto(3, 2) ) {
						BtoY.visible = true;
					}else {
						BtoY.visible = false;
					}
					if (co.connectedto(3, 0) || co.connectedto(0, 3)) {
						YtoR.visible = true;
					}else {
						YtoR.visible = false;
					}
					if (co.connectedto(0, 2) || co.connectedto(2, 0)) {
						RtoB.visible = true;
					}else {
						RtoB.visible = false;
					}
					if (co.connectedto(1, 3) || co.connectedto(3, 1)) {
						GtoY.visible = true;
					}else {
						GtoY.visible = false;
					}
					
					
					
					
					if (STATE == 1) {
						var num:Number = Number(selectedLED.name.charAt(4));
						
						
						if (((co.connectedLeft(num)) || (co.connectedRight(num)))&&(co.numberOfConnectedPairs()>=2)) {
							STATE = 2;
							
						}
					}
					
					if (STATE == 3) {
						if (co.numberOfConnectedPairs() == 0) {
							STATE = 4;
						}
					}
					
					break;
				default: break;
			}
		}
		
		

	}
}